document.addEventListener('DOMContentLoaded', function () {
    const navLinks = document.querySelectorAll('.nav-link');
    const contentAreas = document.querySelectorAll('.content');

    // 模拟点击第一个带有 active 类的导航链接
    const activeLink = document.querySelector('.nav-link.active');
    if (activeLink) {
        const targetId = activeLink.dataset.target;
        const targetContent = document.getElementById(targetId);
        navLinks.forEach(navLink => navLink.classList.remove('active'));
        contentAreas.forEach(content => content.classList.remove('active'));
        activeLink.classList.add('active');
        if (targetContent) {
            targetContent.classList.add('active');
        }
    }

    navLinks.forEach(link => {
        link.addEventListener('click', function (e) {
            navLinks.forEach(navLink => navLink.classList.remove('active'));
            contentAreas.forEach(content => content.classList.remove('active'));
            this.classList.add('active');
            const targetId = this.dataset.target;
            const targetContent = document.getElementById(targetId);
            if (targetContent) {
                targetContent.classList.add('active');
            }
        });
    });
    const tables = document.querySelectorAll('.adminManagement-table');
    tables.forEach(function (table) {
        const selectAll = table.querySelector('.selectAll');
        const rowCheckboxes = table.querySelectorAll('.rowCheckbox');

        selectAll.addEventListener('change', function () {
            const isChecked = this.checked;
            rowCheckboxes.forEach(function (checkbox) {
                checkbox.checked = isChecked;
            });
        });

        rowCheckboxes.forEach(function (checkbox) {
            checkbox.addEventListener('change', function () {
                const allChecked = Array.from(rowCheckboxes).every(function (cb) {
                    return cb.checked;
                });
                selectAll.checked = allChecked;
            });
        });
    });
    //分页
    $(document).ready(function () {
        const rowsPerPage = 7; // 每页显示的行数
        const $tableBody = $('#table-body');
        const rows = $tableBody.find('tr');
        const numPages = Math.max(1, Math.ceil(rows.length / rowsPerPage));
        let currentPage = 1;

        // 显示指定页的数据
        function showPage(pageNumber) {
            currentPage = pageNumber;
            const startIndex = (pageNumber - 1) * rowsPerPage;
            const endIndex = startIndex + rowsPerPage;

            // 隐藏所有行，然后显示当前页的行
            rows.hide();
            rows.slice(startIndex, endIndex).show();

            // 更新分页导航
            updatePagination();
        }

        // 更新分页导航
        function updatePagination() {
            const $pageNav = $('#pageNav');
            $pageNav.empty();

            // 添加"上一页"按钮
            const $prevBtn = $('<a href="#" class="prev-btn">').text('上一页');
            if (currentPage === 1) {
                $prevBtn.addClass('disabled');
            } else {
                $prevBtn.click(function (e) {
                    e.preventDefault();
                    showPage(currentPage - 1);
                });
            }
            $pageNav.append($prevBtn);

            // 添加页码按钮
            const maxVisiblePages = 5;
            let startPage = Math.max(1, currentPage - Math.floor(maxVisiblePages / 2));
            let endPage = startPage + maxVisiblePages - 1;

            if (endPage > numPages) {
                endPage = numPages;
                startPage = Math.max(1, endPage - maxVisiblePages + 1);
            }

            // 添加省略号（如果需要）
            if (startPage > 1) {
                $pageNav.append($('<span>').text('...'));
            }

            for (let i = startPage; i <= endPage; i++) {
                const $pageBtn = $('<a href="#" class="num">').text(i);
                if (i === currentPage) {
                    $pageBtn.addClass('active');
                } else {
                    $pageBtn.click(function (e) {
                        e.preventDefault();
                        showPage(i);
                    });
                }
                $pageNav.append($pageBtn);
            }

            // 添加省略号（如果需要）
            if (endPage < numPages) {
                $pageNav.append($('<span>').text('...'));
            }

            // 添加"下一页"按钮
            const $nextBtn = $('<a href="#" class="next-btn">').text('下一页');
            if (currentPage === numPages) {
                $nextBtn.addClass('disabled');
            } else {
                $nextBtn.click(function (e) {
                    e.preventDefault();
                    showPage(currentPage + 1);
                });
            }
            $pageNav.append($nextBtn);
        }

        // 初始化页面
        if (rows.length > 0) {
            showPage(1);
        }
    });
    //处理按钮
    const clButtons = document.querySelectorAll('.cl');
    clButtons.forEach(function (button) {
        let isBanned = false; // 用于记录每个按钮的禁言状态
        button.addEventListener('click', function () {
            if (isBanned) {
                this.textContent = '未处理';
                this.style.backgroundColor = '#49cc90';
                isBanned = false;
            } else {
                clButtons.textContent = '已处理';
                clButtons.style.backgroundColor ='#89ddfb';
                isBanned = true;

            }
        });
    });

    //驳回和通过
    // 通过按钮
    document.querySelectorAll('.editbtns.text-xl:last-child').forEach(button => {
        button.addEventListener('click', function() {
            const postId = this.getAttribute('data-id');
            handlePostAction(postId, '通过', '/admin/toApproveTheReportedInfo');
            clButtons.textContent = '已处理';
            clButtons.style.backgroundColor ='#89ddfb';
        });
    });

    // 驳回按钮
    document.querySelectorAll('.deletebtn').forEach(button => {
        button.addEventListener('click', function() {
            const postId = this.getAttribute('data-id');
            handlePostAction(postId, '驳回', '/admin/toRejectTheReportInfo');
            clButtons.textContent = '已处理';
            clButtons.style.backgroundColor ='#89ddfb';
        });
    });

    function handlePostAction(postId, actionText, apiUrl) {
        if (confirm(`确定要${actionText}这个帖子吗？`)) {
            $.ajax({
                url: `${apiUrl}?id=${postId}`,
                type: 'GET',
                success: function (response) {
                    alert(`${actionText}成功`);
                    location.reload();
                },
                error: function (error) {
                    console.error(`${actionText}失败`, error);
                    alert(`${actionText}失败，请稍后再试`);
                }
            });
        }
    }

    //添加
    const addButton3 = document.getElementById('addButton3');

    // 获取对应的模态框

    const addModal3 = document.getElementById('addModal3');


});